﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http;
using System.Net.Http.Headers;
using TPLOAWebClient.Resources;
using System.Web.Security;

namespace TPLOAWebClient
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            //client.BaseAddress = new Uri("http://localhost:7279/");
            client.BaseAddress = new Uri("http://tploacloudservice.cloudapp.net/");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            Session["httpclient"] = client;

            if (Page.User.Identity.IsAuthenticated)
            {
                if (Session["username"] != null)
                {
                    HttpResponseMessage response = client.GetAsync("api/members/" + Session["username"]).Result;
                    var member = response.Content.ReadAsAsync<Member>().Result;
                    if (member.UserType.Equals("Student"))
                    {
                        Response.Redirect("studentprofile.aspx");
                    }
                    else if (member.UserType.Equals("Admin"))
                    {
                        Response.Redirect("adminprofile.aspx");
                    }
                    else if (member.UserType.Equals("Staff"))
                    {
                        Response.Redirect("staffprofile.aspx");
                    }
                    else
                    {
                        Response.Redirect("clinicprofile.aspx");
                    }
                }
                else
                {
                    Session.Abandon();
                    FormsAuthentication.SignOut();
                }
            }
        }

        protected void loginBtn_Click(object sender, EventArgs e)
        {
            HttpClient client = (HttpClient)Session["httpclient"];

            string user = usernameTbx.Text;

            HttpResponseMessage response = client.GetAsync("api/members/" + user).Result;
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var member = response.Content.ReadAsAsync<Member>().Result;
                if (passwordTbx.Text.Equals(member.Password))
                {
                    Session["username"] = user;
                    FormsAuthentication.RedirectFromLoginPage(usernameTbx.Text, false);
                    if (member.UserType.Equals("Student"))
                    {
                        Response.Redirect("studentprofile.aspx");
                    }
                    else if (member.UserType.Equals("Admin"))
                    {
                        Response.Redirect("adminprofile.aspx");
                    }
                    else if (member.UserType.Equals("Staff"))
                    {
                        Response.Redirect("staffprofile.aspx");
                    }
                    else
                    {
                        Response.Redirect("clinicprofile.aspx");
                    }
                }
                else
                {
                    statusLbl.Text = "Incorrect Username or Password!";
                }
            }
            else
            {
                statusLbl.Text = "Incorrect Username or Password!";
            }
        }
    }
}